package com.czhlearn.controller;

import com.czhlearn.pojo.Category;
import com.czhlearn.pojo.Result;
import com.czhlearn.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章分类
 *
 * @program: big-event
 * @description:
 * @author: czh
 * @create: 2023-12-19 10:32
 **/
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success();
    }


    /**
     * 文章分类列表
     */
    @GetMapping
    public Result<List<Category>> get(){
        return Result.success(categoryService.get());
    }

    /**
     * 获取文章分类详情
     */

    @GetMapping("/detail")
    public Result<Category> getDetail(Integer id){
        return Result.success(categoryService.getDetail(id));
    }

    /**
     * 更改文章分类
     */

    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }


    /**
     * 删除文章分类
     */
    @DeleteMapping
    public Result delete(Integer id){
        Category detail = categoryService.getDetail(id);
        if (detail==null){
            return Result.error("当前内容不存在");
        }
        categoryService.delete(id);
        return Result.success();
    }



}
